package com.yc.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    @Value("${file.path}")
    private String path;
    @Value("${server.servlet.api-path}")
    private String apiPath;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix(apiPath, c -> c.isAnnotationPresent(RestController.class) || c.isAnnotationPresent(Controller.class));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String pathUrl = "file:" + path.replace("\\", "/");
        registry.addResourceHandler("/file/**").addResourceLocations(pathUrl);
    }
}
